<?php
/* ******************************
 *	SEOoooY aJax actions
 * ******************************
 */

include('../../includes/classes/core.php');
$action = $_REQUEST['act'];
$data = '';

switch ($action) {
	
	case 'document_number_name':
		$rResult = mysql_query("SELECT overhead.waybill_number
								FROM   overhead
				                WHERE overhead.waybill_number != '' AND overhead.is_confirmed = 2");
			
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 2; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
	
		break;
		
	case 'category_statistics':
		$rResult = mysql_query("SELECT DISTINCT production_category.`name`
										FROM production_category
										WHERE `production_category`.`pos_menu_visible`=1");
			
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 2; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
	
		break;
		
    case 'sales_statistics':
		$rResult = mysql_query("SELECT   'საერთო ნავაჭრი' AS `name`
								UNION ALL
								SELECT   `production`.`name`  AS `name`
								  FROM    production
							 LEFT JOIN    pricelist ON pricelist.production_id = production.id
								 WHERE    production.id IN (SELECT pricelist.production_id FROM pricelist)
							  GROUP BY    production.id");
		 
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 2; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
	
		break;
		
//     case 'pos':
// 	    $rResult = mysql_query("SELECT  `production`.`code`,
// 										`production`.`name`
// 								FROM `production` LEFT JOIN `pricelist`
// 								ON `production`.`id` = `pricelist`.`production_id` LEFT JOIN `reserve`
// 								ON `production`.`id` = `reserve`.`production_id`
// 								WHERE `production`.`id` IN (SELECT `production_id` FROM `pricelist`)
// 								GROUP BY `reserve`.`production_id`
// 								HAVING SUM(`reserve`.`quantity` - `reserve`.`sold`) >= 1");
	    
// 		$data = array();
		
// 		$s = 0;
// 		while($aRow = mysql_fetch_array($rResult))
// 		{
// 			for ( $i = 0 ; $i < 2; $i++ )
// 			{
// 				/* General output */
// 				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
// 				$s++;
// 			}
// 		}
		
//         break;

    case 'pos':
    	$status=$_REQUEST['status'];
    	
    	if ($status==1) {
	    	$rResult = mysql_query("SELECT    `production`.`code`
									FROM 	  `production` 
									 
									JOIN `reserve` ON `production`.`id` = `reserve`.`production_id`
		    						where production.actived=1
									GROUP BY  `reserve`.`production_id`
									HAVING SUM(`reserve`.`quantity` - `reserve`.`sold`) >= 1");
	 
			$data = array();
	
			$s = 0;
			while($aRow = mysql_fetch_array($rResult))
				{
					for ( $i = 0 ; $i < 2; $i++ )
						{
							/* General output */
							$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
							$s++;
						}
				}
    	}else if ($status==2){
    		$rResult = mysql_query("SELECT    `production`.`barcode`
									FROM 	  `production`
    		
									JOIN `reserve` ON `production`.`id` = `reserve`.`production_id`
		    						where production.actived=1
									GROUP BY  `reserve`.`production_id`
									HAVING SUM(`reserve`.`quantity` - `reserve`.`sold`) >= 1");
    		
    		$data = array();
    		
    		$s = 0;
    		while($aRow = mysql_fetch_array($rResult))
    		{
    			for ( $i = 0 ; $i < 2; $i++ )
    			{
    				/* General output */
    				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
    				$s++;
    			}
    		}
    	}else {
    		$rResult = mysql_query("SELECT    `production`.`name`
									FROM 	  `production`
    		
									JOIN `reserve` ON `production`.`id` = `reserve`.`production_id`
		    						where production.actived=1
									GROUP BY  `reserve`.`production_id`
									HAVING SUM(`reserve`.`quantity` - `reserve`.`sold`) >= 1");
    		
    		$data = array();
    		
    		$s = 0;
    		while($aRow = mysql_fetch_array($rResult))
    		{
    			for ( $i = 0 ; $i < 2; $i++ )
    			{
    				/* General output */
    				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
    				$s++;
    			}
    		}
    	}
    	
	    

        break;
    case 'pricelist_prod_name':
    	$rResult = mysql_query("SELECT 				`name` 
								FROM 				`production`
								LEFT JOIN 			`reserve`
								ON      			`production`.`id` = `reserve`.`production_id`
    							WHERE actived=1
								GROUP BY   			`production`.`id`
    			               ");
    	
    	$data = array();
    	
    	$s = 0;
    	while($aRow = mysql_fetch_array($rResult))
    	{
    		for ( $i = 0 ; $i < 1; $i++ )
    		{
    			/* General output */
    			$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
    			$s++;
    		}
    	}
    	
    	break;
    case 'subtract_prod_name':
    		$rResult = mysql_query("SELECT `name` FROM `production`
    			               ");
    		 
    		$data = array();
    		 
    		$s = 0;
    		while($aRow = mysql_fetch_array($rResult))
    		{
    			for ( $i = 0 ; $i < 1; $i++ )
    			{
    				/* General output */
    				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
    				$s++;
    			}
    		}
    		 
    		break;    	
    case 'overhead_goods_code':
    	$object_id = $_REQUEST['obj_id'];
    	
    	$rResult = mysql_query("SELECT `P`.`code`
						        FROM `production` AS `P` INNER JOIN `pricelist` AS `PL`
									ON `P`.`id` = `PL`.`production_id` LEFT JOIN `reserve` AS `R`
									ON `P`.`id` = `R`.`production_id`
								WHERE `R`.`object_id` = $object_id
								GROUP BY `P`.`code`
								HAVING SUM(`R`.`quantity`) != SUM(`R`.`sold`)
    			               ");
    	
    	$data = array();
    	
    	$s = 0;
    	while($aRow = mysql_fetch_array($rResult))
    	{
    		for ( $i = 0 ; $i < 1; $i++ )
    		{
    			/* General output */
    			$data[$s] = $aRow[$i];
    			$s++;
    		}
    	}
    	
    	break;
    	
    case 'overhead_goods_name':
    	$object_id = $_REQUEST['obj_id'];
    	
    	if(empty($object_id)){
	    	$rResult = mysql_query("SELECT `name`
							        FROM `production`");
    	}else{
	    	$rResult = mysql_query("SELECT `P`.`name`
							        FROM `production` AS `P` INNER JOIN `pricelist` AS `PL`
										ON `P`.`id` = `PL`.`production_id` LEFT JOIN `reserve` AS `R`
										ON `P`.`id` = `R`.`production_id`
									WHERE `R`.`object_id` = $object_id
									GROUP BY `P`.`code`
									HAVING SUM(`R`.`quantity`) != SUM(`R`.`sold`)
	    			               ");
    	}
    	
    	$data = array();
    	
    	$s = 0;
    	while($aRow = mysql_fetch_array($rResult))
    	{
    		for ( $i = 0 ; $i < 1; $i++ )
    		{
    			/* General output */
    			$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
    			$s++;
    		}
    	}
    	
    	break;

	case 'buyng_goods_code':
    		 
		$rResult = mysql_query("SELECT	`code`
								FROM 	`production`");
    		 
		$data = array();
		$s = 0;
    		 
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
		
    	break;
	case 'buyng_goods_name':
		    		 
		$rResult = mysql_query("SELECT	`name`
								FROM 	`production`
								WHERE actived=1");
    	
		$data = array();
		$s = 0;
    	
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
    	
		break;
	case 'overhead_goods_local_name':
    	$overhead_id = $_REQUEST['id'];
    	
		$rResult = mysql_query("SELECT	`name`
								FROM 	`production`
								WHERE   production.id  NOT IN (SELECT recipie.recipie_id FROM recipie)");
		
		$data = array();		
		$s = 0;
		
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
		
		break;
    case 'overhead_driver_tin':
		$rResult = mysql_query("SELECT	`PR`.`user_id`
								FROM 	`persons` AS `PR` LEFT JOIN `position` AS `PO`
									ON  `PR`.`position` = `PO`.`id`
								WHERE 	`PR`.`position` = 9 && `PO`.`visible` = 'Yes' ");
		
    	$data = array();
    	
    	$s = 0;
    	while($aRow = mysql_fetch_array($rResult))
    	{
    		for ( $i = 0 ; $i < 1; $i++ )
    		{
    			/* General output */
    			$data[$s] = $aRow[$i];
    			$s++;
    		}
    	}
    	
    	break;
    case 'overhead_driver_name':
		$rResult = mysql_query("SELECT	`PR`.`name`
								FROM 	`persons` AS `PR` LEFT JOIN `position` AS `PO`
									ON  `PR`.`position` = `PO`.`id`
								WHERE 	`PR`.`position` = 9 && `PO`.`visible` = 'Yes' ");
		
    	$data = array();
    	
    	$s = 0;
    	while($aRow = mysql_fetch_array($rResult))
    	{
    		for ( $i = 0 ; $i < 1; $i++ )
    		{
    			/* General output */
    			$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
    			$s++;
    		}
    	}
    	
    	break;
    case 'overhead_buyer_tin':
    	$rResult = mysql_query("SELECT	`rs_id`									      
						        FROM 	`partners`
    			               ");
    	
    	$data = array();
    	
    	$s = 0;
    	while($aRow = mysql_fetch_array($rResult))
    	{
    		for ( $i = 0 ; $i < 1; $i++ )
    		{
    			/* General output */
    			$data[$s] = $aRow[$i];
    			$s++;
    		}
    	}
    	
    	break;
    case 'overhead_buyer_name':    			               
    	$rResult = mysql_query("SELECT	`name`									      
						        FROM 	`partners`
    							WHERE   `actived` = 1
    			               ");
    	
    	$data = array();
    	
    	$s = 0;
    	while($aRow = mysql_fetch_array($rResult))
    	{
    		for ( $i = 0 ; $i < 1; $i++ )
    		{
    			/* General output */
    			$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
    			$s++;
    		}
    	}
    	
    	break;    	
	case 'identity_name':
		$rResult = mysql_query("SELECT	`name`
								FROM 	`production`");
		
		$data = array();		
		$s = 0;
		
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
		
		break;
	case 'recipie_name':
		$rResult = mysql_query("SELECT	`name`
					       		FROM 	`production`
								WHERE	`category` = 1
		              ");
		
		$data = array();
		
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
		
		break;
		
	case 'order_recipie_name':
		$rResult = mysql_query("SELECT	`name`
								FROM 	`production`
								WHERE	`name` IN(SELECT `recipie`.`recipie_name` FROM `recipie`) && `category`= 1
	              ");
	
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
	
		break;
	case 'production_name':
		$rResult = mysql_query("SELECT	`name`
					       		FROM 	`production`
								WHERE   `category` != 1 AND `actived`=1");
	
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
		
		break;
	case 'transfer_production_name':
    	$object_id = $_REQUEST['obj_id'];
    	
		$rResult = mysql_query("SELECT 	`name`
								FROM 	`production`
								WHERE 	`id` IN (SELECT `production_id` FROM `reserve` WHERE `object_id` = $object_id)");
		
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
		
		break;
	case 'cartridge_pricelist':
    	$client_id = $_REQUEST['id'];
    	
		$rResult = mysql_query("SELECT DISTINCT `production`.`name`
								FROM			`cartridge_identity` 
								LEFT JOIN		`production` ON `cartridge_identity`.`production_id` = `production`.`id`
								WHERE			`cartridge_identity`.`production_id` NOT IN (SELECT cartridge_pricelist.production_id FROM cartridge_pricelist WHERE `cartridge_pricelist`.`client_id`= $client_id && `cartridge_pricelist`.`actived` = 1)");
	
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
		
		break;
	case 'printer_pricelist':
		$rResult = mysql_query("SELECT DISTINCT `production`.`name`
								FROM 			`printer_identity` LEFT JOIN `production`
								ON 				`printer_identity`.`production_id` = `production`.`id`");
	
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
		
		break;
		
	case 'client_objects':
		$rResult = mysql_query("SELECT		CONCAT(`client`.`name`,'(',`client_objects`.`name`, ')') AS `name`
								FROM 		`client_objects`
								LEFT JOIN 	`client` ON `client`.`id` = `client_objects`.`client_id`
								WHERE		`client_objects`.`actived` = 1");
	
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
	
		break;
		
	case 'client_cartridge':
    	$client_id = $_REQUEST['id'];
    	
		$rResult = mysql_query("SELECT DISTINCT	`production`.`name`
								FROM			`cartridge_identity`
								LEFT JOIN       `production` ON `production`.`id` = `cartridge_identity`.`production_id`
								LEFT JOIN       `cartridge_pricelist` ON `cartridge_pricelist`.`production_id` = `cartridge_identity`.`production_id` && `cartridge_pricelist`.`parts_type`= `cartridge_identity`.`parts_type`
								WHERE			`cartridge_pricelist`.`client_id` = $client_id && `cartridge_pricelist`.`actived` = 1");
	
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
	
		break;
	case 'client_printer':
		$client_id = $_REQUEST['id'];
		 
		$rResult = mysql_query("SELECT DISTINCT	`production`.`name`
								FROM			`printer_identity`
								LEFT JOIN       `production` ON `production`.`id` = `printer_identity`.`production_id`
								LEFT JOIN       `printer_pricelist` ON `printer_pricelist`.`production_id` = `printer_identity`.`production_id` && `printer_pricelist`.`parts_type`= `printer_identity`.`parts_type`
								WHERE			`printer_pricelist`.`client_id` = $client_id && `printer_pricelist`.`actived` = 1");
	
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
	
		break;		
	case 'barcode_client':
		$rResult = mysql_query("SELECT	`name`
					       		FROM 	`client`
								WHERE	`actived` = 1");
		
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
		
		break;
	case 'barcode_cartridge':
		$rResult = mysql_query("SELECT	`PR`.`name`
								FROM	`production` AS `PR`
								WHERE	`PR`.`id` in (SELECT `production_id` FROM `cartridge_identity`) && `PR`.`actived` = 1");
		
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
		
		break;
	case 'barcode_printer':
		$rResult = mysql_query("SELECT	`PR`.`name`
							FROM	`production` AS `PR`
							WHERE	`PR`.`id` in (SELECT `production_id` FROM `printer_identity`) && `PR`.`actived` = 1");
	
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
	
		break;		
	case 'a_letters':
		$rResult = mysql_query("SELECT		CONCAT(`client`.`name`, '(',`client_objects`.`name`, ')', ' №: ა-',`a_letter`.`id`)
								FROM 		`a_letter` 
								LEFT JOIN 	`client_objects` ON `client_objects`.`id` = `a_letter`.`client_object_id`
								LEFT JOIN 	`client` ON `client`.`id` = `client_objects`.`client_id`  && `a_letter`.`id` not in (SELECT `a_letter_id` FROM `work_blank` WHERE `actived` = 1 && a_letter_id!='') ");
	
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
					$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
					$s++;
			}
		}
	
		break;
		
	case 'describe_production':
		$describe_id 	= $_REQUEST['des'];
		$object_id 		= $_REQUEST['ob'];
		$rResult = mysql_query("SELECT  		`production`.`name` AS `name`
								FROM  			`reserve`
								LEFT JOIN     	`production` ON	`reserve`.`production_id` = `production`.`id`
								LEFT JOIN     	`production_unit` ON `production`.`unit` = `production_unit`.`id`
								WHERE			`reserve`.`quantity` - `reserve`.`sold` != 0 AND  production.id NOT IN(SELECT describe_detail.production_id FROM describe_detail WHERE describe_detail.describe_id = '$describe_id') AND `reserve`.`object_id` = '$object_id'
								GROUP BY     	`reserve`.`object_id`, `reserve`.`production_id`");
			
		$data = array();
	
		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}
	
		break;
    default:
       echo 'Action is Null';
}

echo json_encode($data);

?>